package com.petsystem.system.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.petsystem.common.constant.Constants;
import com.petsystem.common.utils.StringUtils;
import com.petsystem.common.core.page.TableDataInfo;
import com.petsystem.common.core.domain.PageQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.petsystem.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.petsystem.system.domain.TPet;
import com.petsystem.system.domain.bo.TOrderBo;
import com.petsystem.system.domain.vo.TOrderVo;
import com.petsystem.system.domain.TOrder;
import com.petsystem.system.mapper.TOrderMapper;
import com.petsystem.system.mapper.TPetMapper;
import com.petsystem.system.service.ITOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单信息Service业务层处理
 *
 * @author petsystem
 * @date 2022-02-27
 */
@Service
public class TOrderServiceImpl extends ServicePlusImpl<TOrderMapper, TOrder, TOrderVo> implements ITOrderService {

    @Autowired
    private TPetMapper tPetMapper;

    @Override
    public TOrderVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<TOrderVo> queryPageList(TOrderBo order, PageQuery pageQuery) {

        Page<TOrderVo> page = baseMapper.selectPageOrderList(pageQuery.build(), order);
        return TableDataInfo.build(page);

//        LambdaQueryWrapper<TOrder> lqw = buildQueryWrapper(bo);
//        Page<TOrderVo> result = pageVo(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
    }

    @Override
    public List<TOrderVo> queryList(TOrderBo bo) {
        LambdaQueryWrapper<TOrder> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<TOrder> buildQueryWrapper(TOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TOrder::getUserId, bo.getUserId());
        lqw.eq(bo.getPetId() != null, TOrder::getPetId, bo.getPetId());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckStatus()), TOrder::getCheckStatus, bo.getCheckStatus());
        return lqw;
    }

    @Override
    public Boolean insertByBo(TOrderBo bo) {
        TOrder add = BeanUtil.toBean(bo, TOrder.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(TOrderBo bo) {
        TOrder update = BeanUtil.toBean(bo, TOrder.class);
        validEntityBeforeSave(update);
        updateById(update);

        //更新宠物领养状态
        TPet pet = new TPet();
        pet.setId(bo.getPetId());
        if(Constants.T_PET_CHCEKSTATUS_YTG.equals(bo.getCheckStatus())){
            pet.setStatus("adopt".equals(bo.getHandleType())?Constants.T_PET_STATUS_YLY:Constants.T_PET_STATUS_YSC);
        }else if(Constants.T_PET_CHCEKSTATUS_WTG.equals(bo.getCheckStatus())){
            pet.setStatus("adopt".equals(bo.getHandleType())?Constants.T_PET_STATUS_WLY:Constants.T_PET_STATUS_WSC);
        }

        Boolean flag = tPetMapper.updateById(pet) > 0;
        return flag;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public boolean adopt(TOrderBo bo) {
        bo.setCheckStatus("0");
        TOrder add = BeanUtil.toBean(bo, TOrder.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        //更新宠物领养状态
//        TPet update = new TPet();
//        update.setId(bo.getPetId());
//        update.setStatus(bo.getHandleType()=="adopt"?:Constants.T_PET_STATUS_YLY);
//        tPetMapper.updateById(update);
        return flag;
    }
}
